<template>
	<view class="withdrawal" :style="{ '--color': vuex_config.themeColor1 }">
		<view class="withdrawal-top">
			<view class="withdrawal-top-title">您的{{nameObj[currentType]}}</view>
			<view class="withdrawal-top-money">{{canMoney}}</view>
			<view class="post_all">总收益￥{{allMoney}}</view>
		</view>
		<view class="withdrawal-money">
			<view class="">
				<text class="amount">提现金额</text>
				<text class="money-mini">(最低￥{{vuex_business.withdraw}}可提现)</text>
			</view>

			<view class="input-money">
				<view class="withdrawal-ipt">
					<view class="money">￥</view>
					<input type="digit" class="money_num" v-model="moneyValue" placeholder="0.00" />
				</view>
				<view class="carry" @click="allWithdrawal">全部提现</view>
			</view>
			<u-line color="#E6E6E6" />

			<view class="choose">请选择收款方式</view>

			<view class="payment-method">
				<view
					:class="['payment-border',item.none?'pay_disabled':'',index === paymentIndex ? 'payment-method-color' : '']"
					v-for="(item, index) in paymentMethod" :key="index" @click="bindPayment(index,item)">
					{{ item.name }}
				</view>
			</view>

			<view class="cash-withdrawal" @click="submit">提现</view>
		</view>

		<view class="explain">
			<u-collapse :head-style="headStyle">
				<view class="link_collapase">
					<u-collapse-item :title="nameObj[currentType] +'明细'" :disabled="true"></u-collapse-item>
					<view @click="goDetail" class="post"></view>
				</view>
				<view class="no_icon">
					<view class="title">
						提现说明
					</view>
					<view class="coll_item">
						{{ currentType==1?vuex_business.bonus_withdraw_explain: vuex_business.withdraw_explain }}
					</view>
				</view>
			</u-collapse>
		</view>
	</view>
</template>

<script>
	import {
		twoDecimalNum
	} from '@/utils/validate.js';
	import {
		uniLinkTo
	} from '@/utils/uniPromise.js'
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		props: {
			currentType: {
				type: Number,
				default: 0
			}, // 0:佣金 1：奖金
		},
		data() {
			return {
				headStyle: {
					borderBottom: '1rpx solid #f5f5f5'
				},
				moneyValue: '', //用户输入的金额
				paymentMethod: [{
						name: '微信零钱',
						msg: '提现到微信',
						none: false,
						type: 1, // 6分红奖金
					},
					{
						name: '消费余额',
						msg: '提现到账户余额',
						type: 3
					},
				],
				paymentIndex: 0, //支付方式下标
				nameObj: {
					0: '佣金',
					1: '奖金'
				}
			};
		},
		mounted() {
			this.changeType(this.currentType)
		},
		watch: {
			currentType(index) {
				this.changeType()
			}
		},
		computed: {
			...mapState(['vuex_count']),
			canMoney() {
				if (this.currentType == 1) {
					// 总的减去已提现
					let num = this.vuex_count.bonus_balance_sum - this.vuex_count.bonus_withdrawal
					num = num > 0 ? num : 0
					let reg = /[0-9]+(\.[0-9]{1,2})?/
					let arr = reg.exec(num)
					return Number(arr && arr[0] ? arr[0] : 0)
				} else {
					return this.vuex_count.sure_cash || 0
				}
			},
			allMoney() {
				let num1 = (this.vuex_count.bonus_balance_sum - this.vuex_count.bonus_withdrawal).toFixed(2)
				num1 = Number(num1 > 0 ? num1 : 0)
				let num2 = num1 + Number(this.vuex_count.sure_cash || 0)
				let reg = /[0-9]+(\.[0-9]{1,2})?/
				let arr = reg.exec(num2)
				return Number(arr && arr[0] ? arr[0] : 0)
			},
			minWith() {
				if (this.currentType == 1) {
					return this.vuex_business.bonus_withdraw
				} else {
					return this.vuex_business.withdraw
				}
			}
		},
		methods: {
			...mapActions(['loadUserCount']),
			changeType() {
				if (this.currentType == 1) {
					this.paymentMethod[0].none = false
					this.paymentMethod[0].type = 6
					this.paymentMethod[1].none = true
					this.paymentIndex = 0
				} else {
					this.paymentMethod[0].none = this.vuex_user.withdrawal != 1
					this.paymentMethod[0].type = 1
					this.paymentMethod[1].none = false
				}
			},
			/* 选择支付方式 */
			bindPayment(index, item) {
				if (item.disabled) {
					return
				}
				this.paymentIndex = index;
			},
			// 全部提现
			allWithdrawal() {
				if (this.canMoney && this.canMoney > 0) {
					this.moneyValue = this.canMoney
				} else {
					this.$toast(`没有${this.nameObj[this.currentType]}可提现`)
				}
			},
			// 查看佣金明细
			goDetail() {
				let url = this.currentType == 1 ? '/package/userSuper/share/bonus/detailList/index' :
					'/package/userSuper/share/distribution/detailList/index'
				this.$linkTo(url)
			},
			// 确定提现
			submit() {
				if (this.moneyValue < this.vuex_business.withdraw) {
					uni.showToast({
						icon: 'none',
						title: `低于最低可提现金额：${this.vuex_business.withdraw}元`
					});
					return;
				}
				if (!twoDecimalNum(this.moneyValue)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的价格数字'
					});
					return;
				} else if (this.moneyValue > this.canMoney) {
					uni.showToast({
						icon: 'none',
						title: `${this.nameObj[this.currentType]}不足`
					});
					return;
				}

				if (this.paymentMethod[this.paymentIndex].type == 1) {
					if (this.moneyValue < 0.3) {
						this.$toast({
							title: '提现微信，金额必须大于0.30元'
						})
						return
					}
				}
				uni.showModal({
					title: `${this.nameObj[this.currentType]}提现`,
					content: `确定提现到${this.paymentMethod[this.paymentIndex].name}?`,
					success: (res) => {
						if (res.confirm) {
							this.sureSub()
						}
					}
				})
			},
			sureSub() {
				this.$loading()
				let remark = this.paymentMethod[this.paymentIndex].msg + this.moneyValue + '元'
				if (this.paymentIndex === 0) {
					remark += ';【' + this.vuex_business.appid + '】'
				}
				let moneyValue = this.moneyValue
				this.moneyValue = ''
				this.$resPost('AddCommission', {
					amount: moneyValue,
					type: this.paymentMethod[this.paymentIndex].type,
					remark: remark
				}).then(() => {
					this.loadUserCount()
					uni.showToast({
						title: '申请成功',
						icon: "success",
						mask: true,
						success: () => {
							setTimeout(() => {
								this.goDetail()
							}, 800)
						}
					})
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.withdrawal {
		width: 100%;
		height: 100%;
		font-size: 26rpx;
	}

	.withdrawal-top {
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		height: 430rpx;
		padding: 60rpx 30rpx 0;
		background-color: var(--color);

		.withdrawal-top-money {
			font-weight: 700;
			margin-top: 29rpx;
			font-size: 70rpx;
			color: #fff;
		}

		.withdrawal-top-title {
			color: #fff;
		}

		.post_all {
			position: absolute;
			right: 16rpx;
			top: 18rpx;
			color: #fff;
			font-size: 26rpx;
		}
	}

	.withdrawal-money {
		background-color: #fff;
		box-shadow: 0px 3rpx 30rpx 0rpx rgba(0, 0, 0, 0.1);
		border-radius: 8rpx;
		margin: -190rpx 30rpx 0;
		padding: 40rpx 40rpx 50rpx;
		position: relative;
		z-index: 10;
	}

	.amount {
		color: #333333;
		font-weight: 500;
		display: inline-block;
		margin-right: 15rpx;
		font-size: 30rpx;
	}

	.money-mini {
		color: #999999;
		font-size: 24rpx;
	}

	.withdrawal-ipt {
		flex: 1;
		display: flex;
		align-items: center;
		font-size: 50rpx;

		.money {
			color: #333;
			font-weight: bold;
			margin-right: 10rpx;
		}

		.money_num {
			font-size: 40rpx;
		}
	}

	.input-money {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 25rpx 0;
	}

	.symbol-money {
		font-size: 60rpx;
		font-weight: 700;
		color: #333333;
	}

	.carry {
		color: var(--color);
	}

	/deep/ .uni-input-input {
		font-size: 50rpx;
	}

	.choose {
		font-size: #333333;
		font-size: 30rpx;
		margin-top: 20rpx;
		font-weight: 500;
	}

	.payment-method {
		display: flex;
		margin-bottom: 50rpx;

		.payment-border {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 200rpx;
			height: 60rpx;
			border: 1rpx solid #ccc;
			color: #999999;
			border-radius: 8rpx;
			margin-right: 60rpx;
			margin-top: 20rpx;

			&.pay_disabled {
				display: none;
				background: #f5f5f5;
			}

			&.payment-method-color {
				border: 1rpx solid var(--color);
				color: var(--color);
			}
		}
	}

	.cash-withdrawal {
		width: 600rpx;
		height: 80rpx;
		background-color: var(--color);
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 40rpx;
		color: #fff;
	}

	.explain {
		padding: 0 30rpx;
		margin-top: 50rpx;

		.link_collapase {
			position: relative;

			.post {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				z-index: 10;
			}

			/deep/ .u-arrow-down-icon {
				transform: rotate(-90deg);
			}
		}

		.no_icon {
			margin-top: 20rpx;

			.title {
				font-weight: bold;
			}

			.coll_item {
				padding: 30rpx 0;
			}
		}

	}
</style>
